package com.sinovatech.ologger.actionlog.aop.annotation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sinovatech.ologger.core.BaseAction;
import com.sinovatech.ologger.util.LogUtil;

public class ActionMethodInvocation implements MethodInvocation {

	protected final Log logger = LogFactory.getLog(getClass());

	private Method method = null;
	private Object[] args = null;
	private Object target = null;

	public Method getMethod() {
		return method;
	}

	public Object[] getArguments() {
		return args;
	}

	public AccessibleObject getStaticPart() {
		return null;
	}

	public Object getThis() {
		return target;
	}

	public Object proceed() throws Throwable {
		return method.invoke(target, target instanceof BaseAction ? null : args);
	}

	public ActionMethodInvocation() {
		super();
	}

	public ActionMethodInvocation(BaseAction baseAction) {
		super();
		try {
			this.method = baseAction.getClass().getMethod("process");
		} catch (NoSuchMethodException e) {
			logger.error(LogUtil.stackTraceToString(e));
		}
		this.args = new Object[] { baseAction };
		this.target = baseAction;
	}

}
